package com.ruoyi.system.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.JjzApplyLog;
import com.ruoyi.system.domain.SysCarInfo;
import com.ruoyi.system.domain.SysWxUser;
import com.ruoyi.system.domain.vo.HphmsVo;
import com.ruoyi.system.domain.vo.JjzResVo;
import com.ruoyi.system.domain.vo.SaveWxUserVo;
import com.ruoyi.system.mapper.SysCarInfoMapper;
import com.ruoyi.system.mapper.SysWxUserMapper;
import com.ruoyi.system.service.IJjzApplyLogService;
import com.ruoyi.system.service.IJjzApplyService;
import com.ruoyi.system.service.ISysWxUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class SysSysWxUserServiceImpl implements ISysWxUserService {
    @Autowired
    private SysWxUserMapper sysWxUserMapper;

    @Autowired
    private SysCarInfoMapper carInfoMapper;

    @Autowired
    private SysConfigServiceImpl sysConfigService;

    @Autowired
    private IJjzApplyService jjzApplyService;

    @Autowired
    private IJjzApplyLogService jjzApplyLogService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysWxUser selectWxUserById(Long id) {
        return sysWxUserMapper.selectWxUserById(id);
    }

    /**
     * 查询微信用户详情
     *
     * @param openid
     * @return
     */
    @Override
    public SaveWxUserVo selectWxUserByOpenid(String openid) {
        SaveWxUserVo saveWxUserVo = new SaveWxUserVo();
        SysWxUser sysWxUser = sysWxUserMapper.selectWxUserByOpenid(openid);
        if (ObjectUtils.isNotEmpty(sysWxUser)) {
            SysCarInfo carInfo = new SysCarInfo();
            carInfo.setWxUserId(sysWxUser.getId());
            List<SysCarInfo> cars = carInfoMapper.selectCarInfoList(carInfo);
            saveWxUserVo.setOpenid(sysWxUser.getOpenid());
            saveWxUserVo.setMobile(sysWxUser.getMobile());
            saveWxUserVo.setPassword(sysWxUser.getPassword());
            saveWxUserVo.setAuthorization(sysWxUser.getAuthorization());
            saveWxUserVo.setRemark(sysWxUser.getRemark());
            List<HphmsVo> list = new ArrayList<>();
            for (SysCarInfo car : cars) {
                HphmsVo hphmsVo = new HphmsVo();
                hphmsVo.setHphm(car.getHphm());
                hphmsVo.setId(car.getId());
                if(car.getStartTime() != null && car.getEndTime() !=null){
                    hphmsVo.setStartTime(car.getStartTime().toLocalDate().toString());
                    hphmsVo.setEndTime(car.getEndTime().toLocalDate().toString());
                    hphmsVo.setCount(LocalDateTimeUtil.betweenPeriod(LocalDateTimeUtil.now().toLocalDate(),car.getEndTime().toLocalDate()).getDays()+1);
                }

                Long expCount = ObjectUtils.isEmpty(car.getExpTime())? 0L: ChronoUnit.DAYS.between(LocalDateTimeUtil.now(),car.getExpTime())+1;
                if(expCount < 10 && expCount > 0){
                    hphmsVo.setExpCount(expCount);
                }
                hphmsVo.setStatus(car.getStatus());
                hphmsVo.setApplyStatus(car.getApplyStatus());
                hphmsVo.setYxqs(car.getYxqs() != null? car.getYxqs().toLocalDate().toString():"");
                if(car.getApplyStatus() != null && 2 == car.getApplyStatus()){
                    JjzApplyLog jjzApplyLog = jjzApplyLogService.selectJjzApplyLog(car.getWxUserId());
                    hphmsVo.setMsg(jjzApplyLog.getMsg());
                }
                list.add(hphmsVo);
            }
            saveWxUserVo.setHphms(list);
        }
        return saveWxUserVo;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysWxUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysWxUser> selectWxUserList(SysWxUser sysWxUser) {
        return sysWxUserMapper.selectWxUserList(sysWxUser);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysWxUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertWxUser(SysWxUser sysWxUser) {
        return sysWxUserMapper.insertWxUser(sysWxUser);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysWxUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateWxUser(SysWxUser sysWxUser) {
        return sysWxUserMapper.updateWxUser(sysWxUser);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWxUserByIds(Long[] ids) {
        return sysWxUserMapper.deleteWxUserByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteWxUserById(Long id) {
        return sysWxUserMapper.deleteWxUserById(id);
    }


    @Override
    public void saveData(SaveWxUserVo vo) {
        SysWxUser sysWxUser = sysWxUserMapper.selectWxUserByOpenid(vo.getOpenid());
        sysWxUser.setPassword(vo.getPassword());
        sysWxUser.setMobile(vo.getMobile());
        sysWxUser.setOpenid(vo.getOpenid());
        if(ObjectUtils.isEmpty(sysWxUser.getAuthorization())){
            sysWxUser.setRemark("录入");
        }
        sysWxUserMapper.updateWxUserByOpenid(sysWxUser);
        if (ObjectUtils.isNotEmpty(vo.getHphms())) {
            for (HphmsVo hphm : vo.getHphms()) {
                SysCarInfo carInfo = new SysCarInfo();
                carInfo.setHphm(hphm.getHphm());
                carInfo.setWxUserId(sysWxUser.getId());
                carInfo.setJsrxm(sysWxUser.getJsrxm());
                carInfo.setJszh(sysWxUser.getJszh());
                if (ObjectUtils.isEmpty(hphm.getId())) {
                    carInfoMapper.insertCarInfo(carInfo);
                } else {
                    carInfo.setId(hphm.getId());
                    carInfoMapper.updateCarInfo(carInfo);
                }
            }
        }
    }

    @Override
    public void initData(String auth, String openid, String expTime, Long carInfoId) {
        HashMap header = new HashMap();
        header.put("Authorization", auth);
        // 获取第三方用户数据
        String res = HttpUtils.sendPost(RuoYiConfig.getJjzPath()+"/pro/applyRecordController/stateList", " ", header);
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject data = jsonObject.getJSONObject("data");
        //身份证
        String sfzmhm = data.getString("sfzmhm");
        JSONArray bzclxx = data.getJSONArray("bzclxx");
        SysWxUser sysWxUser = new SysWxUser();
        sysWxUser.setAuthorization(auth);
        sysWxUser.setRemark("授权");
        sysWxUser.setOpenid(openid);
        for (Object o : bzclxx) {
            JSONObject jsonstr = JSONObject.parseObject(o.toString());
            List<JjzResVo> jjzResVos = JSONArray.parseArray(jsonstr.getString("bzxx"), JjzResVo.class);
            for (JjzResVo jjzResVo : jjzResVos) {
                String hphm = jjzResVo.getHphm();
                //更新用户数据
                sysWxUser.setJsrxm(jjzResVo.getJsrxm());
                sysWxUser.setJszh(jjzResVo.getJszh());
            }
        }
        sysWxUserMapper.updateWxUserByOpenid(sysWxUser);
        SysCarInfo carInfo = new SysCarInfo();
        carInfo.setId(carInfoId);
        LocalDateTime dateTime = LocalDate.parse(expTime).atStartOfDay();
        carInfo.setExpTime(dateTime);
        carInfo.setStatus(true);
        carInfoMapper.updateCarInfo(carInfo);
        //处理新增车牌的数据
        jjzApplyService.editNewData(carInfoId);

    }

    @Override
    public String txt() {
        return sysConfigService.selectConfigByKey("wx.txt");
    }

    @Override
    public int show() {
        return Integer.valueOf(sysConfigService.selectConfigByKey("show"));
    }

    @Override
    public SysWxUser selectWxUserByAuth(String auth) {
        return sysWxUserMapper.selectWxUserByAuth(auth);
    }

}
